<template>
  <div id="max">
    <el-card v-loading="loading" class="card" element-loading-text="拼命加载中" shadow="never">
      <div style="display: flex">
        <div style="font-weight: bold;font-size: 16px;padding: 0 20px">热门文章</div>
        <div style="flex: 1"></div>
        <el-button icon="el-icon-edit" plain type="primary" @click="createBlog">写博客</el-button>
      </div>
      <el-divider></el-divider>
      <div class="article">
        <router-link v-for="(item,index) in data.records" :key="index"
                     :to="{path:'/blog/aetails/'+item.articleId}">
          <el-card body-style="padding: 0px;" class="transition-box" shadow="hover">
            <div class="blockless">
              <img :src="item.articleImg">
              <h3>{{ item.articleTitle }}</h3>
              <p class="texttitle" v-html="item.articleAbstract"></p>
              <div class="blogtime">
                <el-button icon="el-icon-date" style="color: #787171;" type="text">{{ item.articleDate }}</el-button>
                <span style="float: right">
                <el-button icon="el-icon-view" style="color: #787171" type="text">{{ item.articleViews }}</el-button>
                <el-button icon="el-icon-chat-line-square" style="color: #787171"
                           type="text">{{ item.articleCommentCount }}</el-button>
              </span>
              </div>
            </div>
          </el-card>
        </router-link>
      </div>

      <el-pagination
          v-model:current-page="page"
          :total="data.pages*10"
          background
          hide-on-single-page=true,
          layout="prev, pager, next"
          style="text-align: center;margin-top: 50px"
          @current-change="next">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import {ElMessage} from 'element-plus'

export default {
  name: "article",
  data() {
    return {
      page: 1,
      loading: true,
      data: '',
      index: '',
      form: {
        userToken: '',
      },
      user: '',
    }
  },
  created() {
    this.form.userToken = localStorage.getItem('token');
    this.$axios.post("/user/getuser", this.form).then((res) => {
      this.user = res.data.data;
    })
  },
  mounted() {
    this.getArticles();
  },
  methods: {
    getArticles() {
      this.$axios.post("/articles/getArticles", {page: this.page}).then((res) => {
        this.data = res.data.data;
        this.loading = false;
      })
    },
    createBlog() {
      if (this.user === '') {
        ElMessage.warning({
          message: '登录用户才有权限发布博客，请先登录哦！',
          type: 'warning'
        });
      } else {
        this.$router.push('/blog/create')
      }
    },
    next() {
      this.getArticles();
    },
  },
}
</script>

<style scoped>
body a {
  text-decoration: none;
  list-style: none;
}

.card {
  margin: 10px;
  border-radius: 10px;
}

.transition-box {
  display: inline-block;
  margin: 20px 37px;
  font-family: "Josefin Sans", "Noto Sans SC", -apple-system, sans-serif;
  width: 320px;
  height: 384px;
  border-radius: 10px;
}

.blockless img {
  width: 100%;
  height: 200px;
}

.blockless h3 {
  font-weight: 600;
  font-size: 18px;
  padding: 0 20px;
}

.blockless header:hover {
  color: #4facfe;
}

.blockless p {
  padding: 0 20px;
}

.texttitle {
  height: 77px;
  margin-top: -20px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

.texttitle p {
  margin: 0 !important;
}


.blogtime {
  padding: 0 20px;
  border-top: solid 0.5px #ccc;
}

.transition-box:hover {
  transform: translateY(-5px);
}

.transition-box {
  position: relative;
  animation: aaa 0.5s ease-out;
}

.article {
  min-height: 860px;
}

@keyframes aaa {
  from {
    right: 30px;
  }
  to {
    right: 0;
  }
}
</style>